use sea_orm::{
    entity::prelude::*, entity::*,
    DatabaseBackend, MockDatabase, Transaction,
};
use rust_axum::{blog, user};

#[test]
fn fails_when_in_wrong_dir() {
    println!("hello world!");

    let a = 3;
    let b = 1 + 2;
    // 使用断言判断结果的一致性
    assert_eq!(a, b);

    assert_eq!(a, b, "we are testing addition with {} and {}", a, b);
}


#[test]
async fn test_find_cake() -> Result<(), DbErr> {
    // Create MockDatabase with mock query results
    let db = MockDatabase::new(DatabaseBackend::Postgres)
        .append_query_results([
            // First query result
            vec![user::Model {
                user_id: String::from("1"),
                name: "New York Cheese".to_owned(),
                ..Default::default()
            }],
            // Second query result
            vec![
                user::Model {
                    user_id: String::from("1"),
                    name: "New York Cheese".to_owned(),
                    ..Default::default()
                },
                user::Model {
                    user_id: String::from("2"),
                    name: "New York Cheese".to_owned(),
                    ..Default::default()
                },
            ],
        ])
        .append_query_results([
            // Third query result
            [(
                user::Model {
                    user_id: String::from("1"),
                    name: "New York Cheese".to_owned(),
                    ..Default::default()
                },
                blog::Model {
                    blog_id: 2,
                    title: "Apple".to_owned(),
                    user_id: String::from("1"),
                    ..Default::default()
                },
            )],
        ])
        .into_connection();

    // Find a cake from MockDatabase
    // Return the first query result
    assert_eq!(
        user::Entity::find().one(&db).await?,
        Some(user::Model {
            user_id: String::from("1"),
            name: "New York Cheese".to_owned(),
            ..Default::default()
        })
    );

    // Find all cakes from MockDatabase
    // Return the second query result
    assert_eq!(
        user::Entity::find().all(&db).await?,
        [
            user::Model {
                user_id: String::from("1"),
                name: "New York Cheese".to_owned(),
                ..Default::default()
            },
            user::Model {
                user_id: String::from("1"),
                name: "New York Cheese".to_owned(),
                ..Default::default()
            },
        ]
    );

    // Find all cakes with its related fruits
    assert_eq!(
        user::Entity::find()
            .find_also_related(blog::Entity)
            .all(&db)
            .await?,
        [(
            user::Model {
                user_id: String::from("1"),
                name: "New York Cheese".to_owned(),
                ..Default::default()
            },
            Some(blog::Model {
                blog_id: 2,
                title: "Apple".to_owned(),
                user_id: String::from("1"),
                ..Default::default()
            })
        )]
    );

    // Checking transaction log
    assert_eq!(
        db.into_transaction_log(),
        [
            Transaction::from_sql_and_values(
                DatabaseBackend::Postgres,
                r#"SELECT "cake"."id", "cake"."name" FROM "cake" LIMIT $1"#,
                [1u64.into()]
            ),
            Transaction::from_sql_and_values(
                DatabaseBackend::Postgres,
                r#"SELECT "cake"."id", "cake"."name" FROM "cake""#,
                []
            ),
            Transaction::from_sql_and_values(
                DatabaseBackend::Postgres,
                r#"SELECT "cake"."id" AS "A_id", "cake"."name" AS "A_name", "fruit"."id" AS "B_id", "fruit"."name" AS "B_name", "fruit"."cake_id" AS "B_cake_id" FROM "cake" LEFT JOIN "fruit" ON "cake"."id" = "fruit"."cake_id""#,
                []
            ),
        ]
    );

    Ok(())
}